Skip to main content

Deploying a LAMP Environment on Debian 9 Installation Guide

· 15 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction
Have you ever wondered how websites work? Well, they rely on a special combination of software called a "LAMP" stack. It's like a toolbox for building websites and apps. Let's break it down:
  1. "L" stands for Linux, which is the operating system that runs the server.
  2. "A" stands for Apache, which is the software that serves up web pages to visitors.
  3. "M" stands for MariaDB, which is where all the data for the website is stored.
  4. "P" stands for PHP, which is a programming language used to create dynamic content on the website.
In this guide, we'll walk you through setting up a LAMP stack on a Debian 9 server. It's easier than it sounds, and by the end, you'll have your own server ready to host websites and apps!

Preparation for Debian 9 LAMP Setup
Before we begin, ensure you have the following:
  1. A Debian 9 server: This will be your website's home.
  2. A user account with special permissions: This allows you to make important changes.
  3. Basic security measures: Protect your server from unauthorized access.
If you're unsure how to set these up, don't worry. We'll guide you through it.

Setting Up Apache and Security
Apache is a crucial component for hosting websites. It helps to serve web pages to people when they visit your site. Here's how to get it running on your server:

First, we need to install Apache. Don't worry, it's easy! We'll use something called "apt" to do this, which is a tool that helps install software on your server. Just follow along with these steps:
sudo apt update
sudo apt install apache2

Since this is a special command, you'll need to confirm with your regular password to make sure it's you making the changes.

Once you've entered your password, it will show you what it's going to do and how much space it will need. Simply type "Y" and then press ENTER to proceed. That's all there is to it!

Next, if you've already set up the UFW firewall by following the initial server setup instructions, ensure that your firewall lets through traffic for both HTTP and HTTPS.

When you install UFW on Debian 9, it already has some pre-made settings that you can use to adjust your firewall. You can see all of these settings by running:
sudo ufw app list

The WWW profiles help control the ports that web servers use.
Output
Available applications:
. . .
WWW
WWW Cache
WWW Full
WWW Secure
. . .

If you look at the WWW Full profile, you'll see that it allows traffic to go through ports 80 and 443.
sudo ufw app info "WWW Full"

Output

Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)

Ports:
80,443/tcp

Enable incoming traffic for websites using HTTP and HTTPS on this profile.
sudo ufw allow in "WWW Full"

You can quickly check if everything worked by opening your web browser and entering your server's public IP address.
http://your_server_ip

You'll see a page with some information and a test message. This page is provided by Apache on Debian 9 to help you check if everything is set up correctly.

Debain
If you can see this page, it means your web server is set up correctly and can be accessed through your firewall.

If you're not sure what your server's public IP address is, there are a few ways to find it. Usually, it's the same address you use to connect to your server through SSH.

You can find your IP address using a command in the terminal. One way is to use a tool called "iproute2." Just type this command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

When you run the command, you'll get two or three lines back, each showing a different address. They're all correct, but your computer might only be able to use one of them. So, you can try each one to see which works.

Another way to find your IP address is by using a tool called "curl." This tool contacts another server to ask what your IP address is. Here's how to do it:
sudo apt install curl
curl http://icanhazip.com

No matter how you found your IP address, just type it into the address bar of your web browser to see the default Apache page.

Step 2: Configure MariaDB
Now that your web server is set up, let's install MariaDB. MariaDB is like a filing cabinet for your website. It stores and manages all the information your site needs.

In Debian 9, MariaDB is the default database system instead of MySQL. To install it, we'll use a special package called mariadb-server. It's the best way to get MariaDB up and running smoothly.

Once more, use apt to get and install this software:
sudo apt install mariadb-server

Note: You don't need to run sudo apt update again before this command. You already did it earlier when installing Apache, so your computer's list of available software should be current.

This command will also show you a list of packages that will be installed and how much space they'll take up. Just type "Y" to continue.

After the installation is finished, you'll want to run a security script that's already included with MariaDB. This script helps make your database more secure by fixing some settings that might not be safe and restricting access to it. To start the script, just run:
sudo mysql_secure_installation

This will guide you through a series of questions where you can adjust the security settings for your MariaDB installation.The first question will ask you to enter the current database root password. This is like having a master key to access all parts of MariaDB, similar to the administrator account on your computer. Since you've just installed MariaDB and haven't set a password yet, simply press ENTER when prompted.

The next question will ask if you want to set up a password for the database root. Type "N" for "No" and then press ENTER. In Debian, the root account for MariaDB is closely tied to automated system maintenance. It's important not to change the authentication methods for this account. If you do, it could cause problems with future updates and possibly break the database system. We'll show you later how to set up another administrative account if you need password access for your specific situation.

After that, you can type "Y" and then press ENTER to stick with the default settings for the rest of the questions. This will remove some anonymous users and a test database, stop remote root logins, and apply these changes right away so MariaDB follows your instructions.

On new installs of Debian systems, the root MariaDB user is set to authenticate using the unix_socket plugin instead of a password by default. This adds some security and convenience in many situations, but it might complicate things if you need to give external programs (like phpMyAdmin) administrative rights.

Because the root account on the server handles important tasks like managing logs and starting and stopping services, it's not a good idea to change its authentication settings. If you modify the root account credentials in the /etc/mysql/debian.cnf file, it might work at first, but updates could overwrite those changes in the future. Instead, it's recommended to create a new administrative account if you need to use a password for access.

To do this, we'll create a new account called "admin" with the same privileges as the root account, but set up for password authentication. Here's how: Open the MariaDB prompt in your terminal:
sudo mariadb

Now, let's make a new user with special powers like the root user, but this time with a password. You can choose any username and password you like:
Just follow these steps to create the new user:
GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

After creating the new user, we need to make sure the changes are applied right away. To do this, just follow this step:
FLUSH PRIVILEGES;

Then, exit the MariaDB shell as follows:
exit

From now on, whenever you want to use your database with your new administrative user, you'll need to log in as that user using the password you just created. Here's how:
mariadb -u admin -p

Now your database is ready, and you can proceed to install PHP, which is the last part of the LAMP stack.

Step 3: Getting PHP up and running
PHP is what makes your website dynamic, allowing it to process code and display content based on your needs. It can run scripts, fetch data from your MariaDB databases, and deliver the final content to your web server for display.

To install PHP, we'll once again use the apt system. This time, we'll also include some helper packages so that PHP can work seamlessly with Apache and communicate with your MariaDB database.
sudo apt install php libapache2-mod-php php-mysql

PHP installation should go smoothly without any issues. We'll check this soon.

Usually, you'll want to adjust how Apache serves files when someone asks for a directory on your website. Right now, if someone requests a directory, Apache will look for a file named index.html first. Instead, we want Apache to look for an index.php file first.

To do this, follow these steps to open the dir.conf file using a text editor with special permissions:
sudo nano /etc/apache2/mods-enabled/dir.conf

Here's what it will look like:
<IfModule mod_dir.c>
  DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Let's move the PHP index file (highlighted earlier) to the top position after the DirectoryIndex specification, like this:
<IfModule mod_dir.c>
  DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Once you're done, save and close the file by pressing CTRL+X. Confirm the save by typing "Y" and then press ENTER.

After that, you'll need to restart the Apache web server to apply your changes. Just type this:
sudo systemctl restart apache2

You can also see if the Apache web server is running properly by using this command:
sudo systemctl status apache2

Sample Output
● apache2.service - The Apache HTTP Server
 Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
 Active: active (running) since Tue 2018-09-04 18:23:03 UTC; 9s ago
Process: 22209 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Process: 22216 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 22221 (apache2)
  Tasks: 6 (limit: 4915)
 CGroup: /system.slice/apache2.service
         ├─22221 /usr/sbin/apache2 -k start
         ├─22222 /usr/sbin/apache2 -k start
         ├─22223 /usr/sbin/apache2 -k start
         ├─22224 /usr/sbin/apache2 -k start
         ├─22225 /usr/sbin/apache2 -k start
         └─22226 /usr/sbin/apache2 -k start

To add more features to PHP, you can install extra modules. To see what modules and libraries are available, you can use a command called "apt search" along with a tool called "less" that lets you scroll through the list easily. Here's how:
apt search php- | less

You can use the arrow keys to move up and down the list, and press the "Q" key when you're done looking.

These results show extra things you can add to PHP if you want. Each one comes with a brief description to help you understand what it does.
Output

Sorting...
Full Text Search...
bandwidthd-pgsql/stable 2.0.1+cvs20090917-10 amd64
Tracks usage of TCP/IP and builds html files with graphs

bluefish/stable 2.2.9-1+b1 amd64
advanced Gtk+ text editor for web and software development

cacti/stable 0.8.8h+ds1-10 all
web interface for graphing of monitoring systems

cakephp-scripts/stable 2.8.5-1 all
rapid application development framework for PHP (scripts)

ganglia-webfrontend/stable 3.6.1-3 all
cluster monitoring toolkit - web front-end

haserl/stable 0.9.35-2+b1 amd64
CGI scripting program for embedded environments

kdevelop-php-docs/stable 5.0.3-1 all
transitional package for kdevelop-php

kdevelop-php-docs-l10n/stable 5.0.3-1 all
transitional package for kdevelop-php-l10n
…
:

To find out more about what each module does, you can search online for more information. Alternatively, you can see a detailed description of the package by typing:
apt show package_name

You'll see a lot of information, including a field called "Description" that provides a detailed explanation of what each module does.

For instance, if you want to know what the "php-cli" module does, you can type:
apt show php-cli

In addition to a bunch of other details, you'll come across something like this:
Output
…
Description: command-line interpreter for the PHP scripting language (default)
This package provides the /usr/bin/php command interpreter, useful for
testing PHP scripts from a shell or performing general shell scripting tasks.
.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
.
This package is a dependency package, which depends on Debian's default
PHP version (currently 7.0).
…

If you find a program you want to add to your computer, you can use a command called 'apt install' to do it, just like you did before.

For example, if you think you need something called 'php-cli,' you can type:

sudo apt install php-cli

If you want to add multiple programs at the same time, you can do it by writing down the names of each program with a space between them. Just like before, use the 'apt install' command, and list the names of the programs you want to install, like this:
sudo apt install package1 package2 …

Now that you've set up your LAMP stack, it's a good idea to check if everything is working smoothly with PHP. This way, you can catch and fix any problems before you make more changes or try running an application. Let's test your PHP configuration to make sure everything is okay.

Step 4 — Running PHP on your web server and testing it
Now, let's check if PHP is set up correctly on your system by creating a simple PHP script. We'll call it 'info.php.' To make sure Apache can find and show this file, we need to save it in a specific folder called the 'web root.'

Now, let's check if PHP is set up correctly on your system by creating a simple PHP script. We'll call it 'info.php.' To make sure Apache can find and show this file, we need to save it in a specific folder called the 'web root.'
sudo nano /var/www/html/info.php

Once the blank file opens, let's add some code to make it a valid PHP file. Copy and paste the following text into the file. This code is what PHP understands and can execute:
<?php
phpinfo();
?>

After you've added the code, save the changes and close the file.

Now, let's see if your web server can show the result of the PHP script. To do this, open your web browser and go to a specific page. You'll need to use your server's public IP address again.

The page you want to visit is:
http://your_server_ip/info.php

When you go to the page in your web browser, it should look something like this:

php7.0
This page shows basic information about your server using PHP. It's helpful for checking if everything is set up correctly.

If you can view this page in your browser, it means PHP is working as it should.

However, it's a good idea to delete this file after testing because it might share information about your server with unauthorized users. To remove it, just type the following command:
sudo rm /var/www/html/info.php

If you ever need the information on this page again, don't worry! You can always recreate it. For now, it's a good practice to remove the page to keep your server secure. If you ever need it in the future, you can easily make it again.

Conclusion:
Now that you have your LAMP stack ready, you're like a superhero with the power to create all sorts of websites and cool things on your computer!

Feeling excited?

How about checking out some fun websites or even trying to make your very own?

There are so many cool things you can do! If you ever get stuck or need some help, just ask and we're here for you!